package com.example.smartfridge;

import java.util.Date;
import java.util.List;

import com.id2216.fridge.data.DBHelper;
import com.id2216.fridge.data.FridgeItem;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class Fridge extends Activity {

	
    private List<FridgeItem> fridgeItemList;    
    private FridgeAdapter fridegAdapter; 
	DBHelper db;
	
	RadioButton fridge;
	RadioButton list;
	RadioButton more;
	Button addNew;
	Button toList;
	Button delete;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fridge);
		
		fridge = (RadioButton)this.findViewById(R.id.radio0);
		list = (RadioButton)this.findViewById(R.id.radio1);
		more = (RadioButton)this.findViewById(R.id.radio2);
		addNew = (Button)this.findViewById(R.id.button1);
		toList = (Button)this.findViewById(R.id.button2);
		delete = (Button)this.findViewById(R.id.button3);	
	
		list.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent1 = new Intent();
				intent1.setClass(Fridge.this, ShoppingListActivity.class);
				Fridge.this.startActivity(intent1);
				Fridge.this.finish();
				
				
			}
		});
		more.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent2 = new Intent();
				intent2.setClass(Fridge.this, More.class);
				Fridge.this.startActivity(intent2);
				Fridge.this.finish();
				
			}
		});
		
		
        // setup database
        
        db = new DBHelper(this.getApplicationContext());
        fridgeItemList = db.getAllFridgeItem();
        
        fridegAdapter = new FridgeAdapter(this, R.layout.fridge_item_cell, fridgeItemList);
        
        // table
        
        ListView listView = (ListView) this.findViewById(R.id.fridgeListView); 
        listView.setAdapter(fridegAdapter); 
        listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) 
			{
				Date date = new Date();
				final FridgeItem item = fridgeItemList.get(position);
				if( item.getExpire().before(date))
				{
	                // Setting Dialog
					AlertDialog.Builder alertDialog = new AlertDialog.Builder(Fridge.this);
					 
	                alertDialog.setTitle("Expired item ...");	 
	                alertDialog.setMessage("Do you want to move it to shopping list?");
	 
	                alertDialog.setNeutralButton("REMOVE", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	if(db.deleteFridgeItem(item) > 0 )
	                    	{
		                    	fridgeItemList.remove(position);
		                    	fridegAdapter.notifyDataSetChanged();
		                    	Toast.makeText(getApplicationContext(), "Item removed from the list", Toast.LENGTH_SHORT).show();	                    	
	                    	}
	                    }
	                });
	 
	                alertDialog.setNegativeButton("YES", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	if(db.moveFridgeItemToShopping(item, false) > 0)
	                    	{
		                    	fridgeItemList.remove(position);
		                    	fridegAdapter.notifyDataSetChanged();
			                    Toast.makeText(getApplicationContext(), "Item moved to the shopping list", Toast.LENGTH_SHORT).show();	                    		
	                    	}
	                    }
	                });
	 
	                alertDialog.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	
	                    }
	                });

	
	 
	                // Showing Alert Message
	                alertDialog.show();
				}
				
			}
        	
        })  ; 
        
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.fridge, menu);
		return true;
	}
	

}
